/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package search;

import cont.UIController;
import drawables.DrawableManager;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Point2D;
import javafx.scene.Cursor;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.DragEvent;
import javafx.scene.input.MouseDragEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.GridPane;
import objects.AudioClip;
import objects.Track;

/**
 *
 * @author Irzh
 */
public class SearchResultFactory {
    
    final private GridPane gp;
    final private UIController uic;
    private int rowIndex = 1;
    
    
    
    public SearchResultFactory(GridPane gp , UIController uic){
        this.gp = gp;
        this.uic = uic;
    }
    
    public void addSearchResult(AudioClip ac){
        final ImageView loadingImage = createLoadingImage();
        gp.add(createTitleLabel(ac, loadingImage), 0  , rowIndex);
        gp.add(new Label(ac.getUplaoder()), 1 , rowIndex);
        gp.add(new Label(ac.getInstrument()), 2 , rowIndex);
        gp.add(new Label(ac.getGenre()), 3 , rowIndex);
        gp.add(new Label(String.valueOf(ac.getRating())), 4 , rowIndex);
        gp.add(new Label(String.valueOf(ac.getBpm())), 5 , rowIndex);
        gp.add(new Label(ac.getKey()), 6 , rowIndex);
        gp.add(createAddButton(ac , loadingImage), 7 , rowIndex);
        gp.add(createPlayButton(ac), 8 , rowIndex);
        gp.add(loadingImage, 9 , rowIndex);
        
        rowIndex++;
    }
    
    
    private Label createTitleLabel(final AudioClip ac, final ImageView loadingImage){
        final Label titleLabel = new Label(ac.getTitle());

        titleLabel.setCursor(Cursor.HAND);
        
        titleLabel.setOnMousePressed(new EventHandler<MouseEvent>(){

            @Override
            public void handle(MouseEvent event) {

                
            }
            
        });
        
        titleLabel.setOnMouseDragged(new EventHandler<MouseEvent>(){

            @Override
            public void handle(MouseEvent event) {
                Point2D p2d1 = titleLabel.localToScene(event.getX(), event.getY());
                Point2D p2d2 = uic.getCanvas().sceneToLocal(p2d1);
                DrawableManager.get().getTrackLine().setXQuatized((int)p2d2.getX());
                //System.out.println("move: " + p2d2.getX() + " " + p2d2.getY());
            }
            
        });
        
        
        titleLabel.setOnMouseReleased(new EventHandler<MouseEvent>(){

            @Override
            public void handle(MouseEvent event) {
                Point2D p2d1 = titleLabel.localToScene(event.getX(), event.getY());
                Point2D p2d2 = uic.getCanvas().sceneToLocal(p2d1);
                //System.out.println("drop: " + p2d2.getX() + " " + p2d2.getY());
                
                AudioClip cloned = ac.clone();
                cloned.setLoadingImage(loadingImage);
                cloned.setX((int)p2d2.getX());
                Track t = DrawableManager.get().getTrack((int)p2d2.getX(), (int)p2d2.getY());
                if(t!=null){
                    DrawableManager.get().getTrack((int)p2d2.getX(), (int)p2d2.getY()).addAudioClip(cloned);
                    loadingImage.setVisible(true);
                }
            }
            
        });
        return titleLabel;
    }
    
    
    private Button createAddButton(final AudioClip ac, final ImageView loadingImage){
        Button b = new Button();
        b.setText("Add");
        b.setOnAction(new EventHandler<ActionEvent>(){
            @Override
            public void handle(ActionEvent event) {
                //uic.addAudioClip(0, 0, ac.clone());
                AudioClip cloned = ac.clone();
                cloned.setLoadingImage(loadingImage);
                cloned.setX(DrawableManager.get().getTrackLine().getX());
                UIController.getCurrentTrack().addAudioClip(cloned);
                loadingImage.setVisible(true);
                
            }
        });
        return b;
    } 
    
    private Button createPlayButton(final AudioClip ac){
        Button b = new Button();
        b.setText("play");
        b.setOnAction(new EventHandler<ActionEvent>(){
            @Override
            public void handle(ActionEvent event) {
                ac.previewPlay();
            }
        });
        return b;
    } 
    
    private ImageView createLoadingImage(){
        ImageView iv = new ImageView();
        iv.setVisible(false);
        Image image = new Image(getClass().getResourceAsStream("/resources/imgs/loading.gif"));
        iv.setImage(image);
        return iv;
    }
}
